home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / LSR.C < prev    next >
Text File  |  1992-12-02  |  10KB  |  419 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  *
  55.  *    lsr        - list resource 
  56.  */
  57.  
  58. #include    <ResourceMgr.h>
  59.  
  60. #include    "SystemPub.h"
  61. #include    "Proc.h"
  62. #include    "ShellPub.h"
  63. #include    "Path.h"
  64.  
  65. /*******************************************************************
  66.  *
  67.  *    Function LSR
  68.  *
  69.  *    PathName Callback function
  70.  *
  71.  *    usage LSR [options] [names]    
  72.  *
  73.  *
  74.  *******************************************************************/
  75.  
  76. extern     char resName[], resAttrStr[];
  77.  
  78. #define        lsrResType        (**MyShell).Proc[ProcID].long0
  79. #define        lsrSize            (**MyShell).Proc[ProcID].long1
  80. #define        lsrIDStart        (**MyShell).Proc[ProcID].int0
  81. #define        lsrIDEnd        (**MyShell).Proc[ProcID].int1
  82. #define        checkT            (**MyShell).Proc[ProcID].int2
  83.  
  84. #define        lsrTypes        (**MyShell).Proc[ProcID].bflags.f0
  85. #define        lsrIDs            (**MyShell).Proc[ProcID].bflags.f1
  86. #define        lsrData            (**MyShell).Proc[ProcID].bflags.f2
  87. #define        lsrAbort        (**MyShell).Proc[ProcID].bflags.f3
  88.  
  89.  
  90. GetResAttrString( Handle resHand )
  91. {
  92. int16        resAttr, i = 0;
  93.  
  94.     resAttr = GetResAttrs( resHand );
  95.  
  96.     resAttrStr[ i++ ] = (resAttr & resSysRef         ? 'S' : 's');
  97.     resAttrStr[ i++ ] = (resAttr & resSysHeap         ? 'H' : 'h');
  98.     resAttrStr[ i++ ] = (resAttr & resPurgeable     ? 'U' : 'u');
  99.     resAttrStr[ i++ ] = (resAttr & resLocked         ? 'L' : 'l');
  100.     resAttrStr[ i++ ] = (resAttr & resProtected     ? 'R' : 'r');
  101.     resAttrStr[ i++ ] = (resAttr & resPreload         ? 'P' : 'p');
  102.     resAttrStr[ i++ ] = (resAttr & resChanged         ? 'C' : 'c');
  103.  
  104.     resAttrStr[ i ] = '\0';
  105. }
  106.  
  107. /*******************************************************************/
  108.  
  109. void        lsrDumpType( WHandle ShellWh, int16 ProcID, OsType theType,
  110.                  int16 numRes )
  111. {
  112. OsType        aType;
  113. Boolean        dropOut = FALSE;
  114. int16        resID;
  115. Handle        h;
  116. char        str[ 64 ];
  117. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  118.  
  119.     while( --numRes && !dropOut && !UserAbort() )
  120.         {
  121.         h = Get1IndResource( theType, numRes );
  122.         
  123.         if( h )
  124.             {
  125.             GetResInfo( h, &resID, &aType, str );
  126.             
  127.             if( checkT == 2 )
  128.                 {
  129.                 if( resID < lsrIDStart )
  130.                     continue;
  131.                 else if( resID > lsrIDEnd )
  132.                         continue;
  133.                 }
  134.             else if( checkT == 1 )
  135.                 if( resID != lsrIDStart )
  136.                     continue;
  137.             
  138.             GetResAttrString( h );
  139.             lsrSize = SizeResource( h );
  140.             
  141.             procPrintf( ShellWh, ProcID, "         %2d : %6d %s %6ld %ps\n",
  142.                  numRes, resID, resAttrStr, lsrSize, str );
  143.             }
  144.         if( UserAbort() )
  145.             dropOut = TRUE;
  146.         }
  147. }
  148.  
  149. /*******************************************************************/
  150.  
  151. void        lsrDumpName( WHandle ShellWh, int16 ProcID, 
  152.                  OsType theType, char *theName )
  153. {
  154. OsType        aType;
  155. Handle        h;
  156. char        str[ 64 ];
  157. int16        resID;
  158. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  159.  
  160.     strcpy( str, theName );
  161.     CtoPstr( str );
  162.     h = Get1NamedResource( theType, str );
  163.     
  164.     if( h )
  165.         {
  166.         GetResInfo( h, &resID, &aType, str );
  167.     
  168.         if( checkT == 2 )
  169.             {
  170.             if( resID < lsrIDStart )
  171.                 return;
  172.             else if( resID > lsrIDEnd )
  173.                     return;
  174.             }
  175.         else if( checkT == 1 )
  176.             if( resID != lsrIDStart )
  177.                 return;
  178.             
  179.         GetResAttrString( h );
  180.         lsrSize = SizeResource( h );
  181.         
  182.         procPrintf( ShellWh, ProcID, "          : %6d %s %6ld %ps\n",
  183.              resID, resAttrStr, lsrSize, str );
  184.         }
  185. }
  186.  
  187. /*******************************************************************/
  188.  
  189. void        LSRCallBack( WHandle ShellWh, int16 ProcID, char *path,
  190.                 char *last, pathType what, int16 vRefNum, int32 dirID )
  191. {
  192. char        str[ 256 ];
  193. OSErr        err;
  194. int16        refNum, currFile, numTypes, numRes, resID, resFileWasOpen = FALSE;
  195. OsType        theType, aType;
  196. Handle        h;
  197. Boolean        dropOut = FALSE, fileName = TRUE;
  198. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  199.  
  200.     if( what == pathIsFile )
  201.         {
  202.         strcpy( str, last );
  203.         CtoPstr( str );
  204.         
  205.         err = HSetVol( NULL, vRefNum, dirID );
  206.         
  207.         if( err )
  208.             return;
  209.             
  210.         currFile = CurResFile();
  211.         if( ResFileOpen( last, vRefNum, dirID, &refNum ) )
  212.             resFileWasOpen = TRUE;
  213.         else
  214.             refNum = OpenResFile( str );
  215.         
  216.         if( refNum != (-1) )
  217.             {
  218.             UseResFile( refNum );
  219.             SetResLoad( FALSE );
  220.             numTypes = Count1Types() + 1;
  221.             
  222.             while( --numTypes && !dropOut && !lsrAbort )
  223.                 {
  224.                 if( UserAbort() )
  225.                     lsrAbort = TRUE;
  226.                     
  227.                 Get1IndType( &theType, numTypes );    /* What type is this ? */
  228.                 TypeToStr( theType, str );
  229.                 
  230.                 numRes = Count1Resources( theType ) + 1;
  231.                 
  232.                 if( lsrResType && (lsrResType != theType ) )
  233.                     continue;
  234.                 
  235.                 if( fileName )
  236.                     {
  237.                     procPrintf( ShellWh, ProcID, "lsr : %s\n", last );
  238.                     fileName = FALSE;
  239.                     }
  240.                     
  241.                 procPrintf( ShellWh, ProcID,
  242.                     "    %s (%d)\n", str, numRes - 1 );
  243.                 procPrintf( ShellWh, ProcID,
  244.                     "    -- Index --- ID - Flags - Size -- Name ------\n" );
  245.                     
  246.                 if( *resName )
  247.                     lsrDumpName( ShellWh, ProcID, theType, resName );
  248.                 else
  249.                     lsrDumpType( ShellWh, ProcID, theType, numRes );
  250.                 }
  251.                 
  252.             if( !resFileWasOpen )
  253.                 CloseResFile( refNum );
  254.                 
  255.             SetResLoad( TRUE );
  256.             UseResFile( currFile );
  257.             }
  258.         else if( (err = ResError()) && (err != eofErr) )
  259.             procPrintf( ShellWh, ProcID, "lsr : error openning file %s (%d)\n", 
  260.                 last, err );
  261.         }
  262. }
  263.  
  264. /*******************************************************************/
  265.  
  266. void        LSRFile( WHandle ShellWh, int16 ProcID, char *argument )
  267. {
  268. ShellWindRec    **MyShell;
  269.  
  270.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  271.  
  272.     ExpandPath( ShellWh, ProcID, argument, (ProcPtr) LSRCallBack,
  273.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  274.  
  275.     ResetShellPWD( ShellWh );
  276. }
  277.  
  278. /*******************************************************************/
  279.  
  280. Boolean            DoLSR( int16 ProcToken, WHandle ShellWh, int16 ProcID,
  281.                     char *string )
  282. {
  283. int16            i, argc;
  284. char            *cp, argument[ 256 ], badIDs = FALSE;
  285. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  286.  
  287.     switch( ProcToken )
  288.         {
  289.         case    PROC_INIT    :
  290.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  291.             break;
  292.             
  293.         case    PROC_TERM    :
  294.         case    PROC_BREAK    :
  295.             lsrAbort = TRUE;
  296.             /* Tell the shell that we're done */
  297.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  298.             /* Turn ourself off */
  299.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  300.             break;
  301.             
  302.         case    PROC_STDIN    :
  303.             if( (**MyShell).Proc[ ProcID ].flags )
  304.                 {
  305.                 (**MyShell).Proc[ ProcID ].flags = FALSE;        
  306.  
  307.                 /* get arguments */
  308.                 argc = (**MyShell).Proc[ ProcID ].argc;
  309.                 lsrTypes    = TRUE;
  310.                 lsrIDs        = TRUE;
  311.                 lsrData        = TRUE;
  312.                 checkT        = 0;
  313.                 lsrIDStart    = 0;
  314.                 lsrIDEnd    = 0;
  315.                 lsrResType    = 0L;        /* no type specifier */
  316.                 lsrAbort     = FALSE;
  317.                 *resName    = '\0';
  318.                 
  319.                 
  320.                 for( i = 1; i < argc; i++ )
  321.                     {
  322.                     GetArgv( ShellWh, ProcID, i, argument );
  323.                     cp = argument;
  324.         
  325.                     if( *cp++ == '-' )
  326.                         while( *cp )
  327.                             switch( *cp++ )
  328.                                 {
  329.                                 case    'n'    :    /* Name */
  330.                                     i++;
  331.                                     GetArgv( ShellWh, ProcID, i, resName );
  332.                                     break;
  333.  
  334.                                 case    'i'    :    /* id's */
  335.                                     i++;
  336.                                     if( i < argc )
  337.                                         {
  338.                                         char    *cp, nc, temp[ 64 ], num[ 64 ];
  339.                                         int32    lnum;
  340.                                         
  341.                                         GetArgv( ShellWh, ProcID, i, temp );
  342.                                         
  343.                                         cp = temp;
  344.                                         while( *cp && (*cp != ',') )
  345.                                             cp++;
  346.                                         
  347.                                         nc = *cp;
  348.                                         
  349.                                         if( *cp )
  350.                                             *cp = '\0';
  351.                                         
  352.                                         CtoPstr( temp );
  353.                                         if( GoodNum( temp ) )
  354.                                             {
  355.                                             StringToNum( temp, &lnum );
  356.                                             lsrIDStart = (int16) lnum;
  357.                                             checkT++;
  358.  
  359.                                             if( nc )
  360.                                                 {
  361.                                                 cp++;
  362.                                                 CtoPstr( cp );
  363.                                                 if( GoodNum( cp ) )
  364.                                                     {
  365.                                                     StringToNum( cp, &lnum );
  366.                                                     lsrIDEnd = (int16) lnum;
  367.                                                     checkT++;
  368.                                                     }
  369.                                                 else
  370.                                                     badIDs = TRUE;
  371.                                                 }
  372.                                             }
  373.                                         else
  374.                                             badIDs = TRUE;
  375.                                         }
  376.                                     break;
  377.  
  378.                                 case    't'    :        /* Type */
  379.                                     i++;
  380.                                     if( i < argc )
  381.                                         {
  382.                                         char    temp[ 256 ];
  383.                                         
  384.                                         GetArgv( ShellWh, ProcID, i, temp );
  385.                                         lsrResType = StrToType( temp );
  386.                                         }
  387.                                     break;
  388.                                 }
  389.                     }
  390.  
  391.                 if( badIDs )
  392.                     procPrintf( ShellWh, ProcID, "lsr : bad resource ID\n" );
  393.                 else
  394.                     for( i = 1; i < argc; i++ )
  395.                         {
  396.                         GetArgv( ShellWh, ProcID, i, argument );
  397.                         if( *argument != '-' )
  398.                             LSRFile( ShellWh, ProcID, argument );
  399.                         else
  400.                             switch( argument[ 1 ] )
  401.                                 {
  402.                                 case    'n'    :
  403.                                 case    'i'    :
  404.                                 case    't'    :
  405.                                     i++;
  406.                                     break;
  407.                                 }
  408.                         }
  409.  
  410.                 /* Tell the shell that we're done */
  411.                 SendOutToken(  ShellWh, ProcID, PROC_BREAK );
  412.                 
  413.                 /* Turn ourself off */
  414.                 (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  415.                 return( FALSE );
  416.                 }
  417.         }
  418. }
  419.